<template>
    <div>
        <el-card>
            <el-form :model="formInline" label-width="70px" class="demo-form-inline">
                <el-form-item label="旧密码">
                    <el-input v-model="formInline.olpassword" show-password placeholder="请输入旧密码" />
                </el-form-item>
                <el-form-item label="新密码">
                    <el-input v-model="formInline.newpassword" show-password placeholder="请输入新密码" />
                </el-form-item>
                <el-form-item label="确认密码">
                    <el-input v-model="formInline.doublepassword" show-password placeholder="请再次输入新密码" />
                </el-form-item>
                <el-form-item label="">
                    <el-button type="primary" class="submitBtn" @click="onSubmit"> 修改密码 </el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>
<script setup lang="ts">
import { reactive } from "vue";
import { Md5 } from "ts-md5";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import { updateUserpwd } from "@/utils/api";
const router = useRouter();
const formInline = reactive({
    olpassword: "",
    newpassword: "",
    doublepassword: "",
});
const onSubmit = () => {
    if (!formInline.olpassword) {
        ElMessage.error("旧密码不能为空");
        return;
    }
    if (!formInline.newpassword) {
        ElMessage.error("新密码不能为空");
        return;
    }
    if (formInline.newpassword != formInline.doublepassword) {
        ElMessage.error("两次密码输入不一致");
        return;
    }
    const data = {
        olpassword: get_md5(formInline.olpassword),
        newpassword: get_md5(formInline.newpassword),
        doublepassword: get_md5(formInline.doublepassword),
    };
    // // 定义MD5对象
    // const md5: any = new Md5();
    // md5.appendAsciiStr(formInline.olpassword);
    // data.olpassword = md5.end();
    // md5.appendAsciiStr(formInline.newpassword);
    // data.newpassword = md5.end();
    // md5.appendAsciiStr(formInline.doublepassword);
    // data.doublepassword = md5.end();

    updateUserpwd(data).then((res: any) => {
        ElMessage.success("修改成功");
        router.back();
    });
    // formInline
};
const get_md5 = (pwd: string) => {
    const md5: any = new Md5();
    md5.appendAsciiStr(pwd);
    return md5.end();
};
</script>
<style scoped lang="scss">
.demo-form-inline {
    width: 600px;
    position: relative;
    transform: translateX(-50%);
    left: 50%;
}
</style>
